<template>
  <view>
    <view class="goods-list">
       <view v-for="(good,g1) in goodsList" :key="g1" @click="gotoGoodDetail(good)">
          <my-goods :good="good"></my-goods>
       </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求参数对象
        queryObj:{
          //查询关键字
          query:'',
          //商品分类id
          cid:'',
          //页码值
          pagenum:1,
          //每页显示多少数据
          pageSize:10
        },
        //商品列表的数据
        goodsList:[],
        //总数量,用来实现分页
        total:0,
        //判断是否在请求的数据
       isLoading:false
      }
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid 
       this.getGoodsList()
    },
    methods: {
      //获取商品列表数据
     async getGoodsList(cb){
       //打开节流阀
         this.isLoading=true
        const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
          this.isLoading=false
         cb&&cb()
        if(res.meta.status !== 200) return uni.$showMsg
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      //下拉获取数据加判断
      onRearchBootom(){
        if(this.queryObj.pagenum*this.queryObj.pageSize >= total)return uni.$showMsg('数据已全部加载完毕')
        if(this.isLoading)return
        //让页面自增+1
        this.queryObj.pagenum++
        this.getGoodsList()
      },
      onPullDownRefresh(){
        //重置关键数据
        this.queryObj.pagenum =1
        this.total = 0
        this.isLoading = false
        this.goodsList= []
        
        //重新发起数据请求
        this.getGoodsList(()=>uni.stopPullDownRefresh())
      },
      gotoGoodDetail(good){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + good.goods_id
        })
      }
    }
  }
</script>

<style lang="scss">
.good-item{
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #fff;
  .good-item-left{
    .good-pic{
      display: block;
      margin-right: 5px;
      width: 100px;
      height: 100px;
    }
  }
  .good-item-right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .good-name{
      font-size: 13px;
    }
    .good-info-box{
      .good-price{
        font-size: 16px;
        color: red;
      }
    }
  }
}
</style>
